10 #define D(x) cout << #x " is " << (x) << endl
12 typedef unsigned long long uint64
;
14 const int BUFSIZE
= 512;
22 node(bool end
) : end(end
) {
27 if (left
== NULL
) left
= new node(false);
31 if (right
== NULL
) right
= new node(false);
38 if (e
== 64) return 0;
39 return uint64(1) << e
;
42 void clean(node
* cur
){
43 if (cur
== NULL
) return;
51 Returns how many strings were counted under the tree rooted at cur, including it.
53 uint64
alreadyCounted(node
* cur
){
54 if (cur
== NULL
) return 0;
55 uint64 left
= alreadyCounted(cur
->left
);
56 uint64 right
= alreadyCounted(cur
->right
);
57 uint64 ret
= left
+ right
;
59 //count strings matched to this one
60 ret
+= pow2(m
- cur
->s
.size()) - left
- right
;
70 while (getchar() != '\n');
71 if (n
== 0 && m
== 0) break;
72 node
* root
= new node(true);
73 for (int i
=0; i
<n
; ++i
){
75 fgets(buf
, BUFSIZE
, stdin
);
83 next
= cur
->getLeft();
84 next
->s
= cur
->s
+ '0';
85 }else if (buf
[j
] == '1'){
86 next
= cur
->getRight();
87 next
->s
= cur
->s
+ '1';
89 printf("Bad character %c at query %d, position %d\n", buf
[j
], i
, j
);
95 fgets(buf
, BUFSIZE
, stdin
);
97 sscanf(buf
, "%d", &k
);
98 for (int i
=0; i
<k
; ++i
){
99 fgets(buf
, BUFSIZE
, stdin
);
100 buf
[strlen(buf
)-1] = 0;
103 for (int j
=0; buf
[j
] != '*'; ++j
){
106 }else if (buf
[j
] == '1'){
110 uint64 ans
= pow2(m
- cur
->s
.size()) - alreadyCounted(cur
->left
) - alreadyCounted(cur
->right
);
113 while (getchar() != '\n'); //empty line